﻿RequestsApp.controller('HomeCtrl', ['$scope', '$rootScope', '$location', 'SharePointJSOMService', '$routeParams', '$log', function ($scope, $rootScope, $location, SharePointJSOMService, $routeParams, $log) {
    $scope.Requests = [];
    $scope.HomeController = {};
    $scope.HomeController.Requests = {};
    
    //#region get requests list
    SharePointJSOMService.getRequests()
    .then(function (requests) {
        $scope.Requests = requests;
        $scope.HomeController.Requests.RequestsLoaded = true;
    },
    function (err) {
        console.info(JSON.stringify(err));
    });
    //#endregion

    //#region delete request
    $scope.deleteRequest = function($event, id) {
        $event.preventDefault();
        SharePointJSOMService.deleteRequest(id)
            .then(function(id) {
                for (var i = 0; i < $scope.Requests.length; i++) {
                    if ($scope.Requests[i].Id == id) {
                        $scope.Requests.splice(i);
                        break;
                    }
                }
            },
                function(err) {
                    $log.error(JSON.stringify(err));
                    noty({ text: JSON.stringify(err) });
                });
    };
    //#endregion
}]);